package Homework;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;

import demo08.Student;

/*在你生成的文件中，生成10个男生或者10个女生的姓名.
被点到的学生的姓名不会再被点到，如果都点完了，才开始第二轮点名*/
public class work09 {
    public static void main(String[] args) throws Exception {
/*		ArrayList<String> list = new ArrayList();
		BufferedReader br1 = new BufferedReader(new FileReader("names.txt"));
		String line;
		while ((line = br1.readLine()) != null) {
			list.add(line);
		}
		System.out.println(list);
		ArrayList<String> list1 = new ArrayList();
		list1.addAll(list);
		for (int count = 1; count < 3; count++) {
			System.out.println("第"+count+"轮点名");
			while (list.size() > 0) {
				Random r = new Random();
				int i = r.nextInt(list.size());
				System.out.println(list.get(i));
				list.remove(i);
			}
			list.addAll(list1);
		}*/
        randomCallName();
    }

    public static void randomCallName() throws Exception, IOException{
        ArrayList<Student> list = new ArrayList<>();
        BufferedReader br = new BufferedReader(new FileReader("names5.txt"));
        String line;
        while((line=br.readLine())!=null){
            String[] arr = line.split("-");
            Student s=new Student(arr[0], arr[1], Integer.parseInt(arr[2]),Double.parseDouble(arr[3]) );
            list.add(s);
        }
        br.close();
        //System.out.println(list);

        //计算权重的总和
        double weight = 0;
        for (Student s : list){
            weight = weight + s.getWeight();
        }
        //计算每一个人的实际占比
        double[] arr = new double[list.size()];
        int index = 0;
        for (Student s : list){
            arr[index] = s.getWeight()/weight;
            index++;
        }
        //System.out.println(Arrays.toString(arr));
        //计算每一个人的权重占比范围
        for(int i=1;i<arr.length;i++){
            arr[i]=arr[i]+arr[i-1];
        }
        //System.out.println(Arrays.toString(arr));
        //随机抽取
        double number = Math.random();
        //二分查找
        int index2 = -Arrays.binarySearch(arr, number)-1;
        Student student = list.get(index2);
        System.out.println(student);
        //修改学生的权重
        student.setWeight(0);

        //把集合又重新写会文件
        BufferedWriter bw=new BufferedWriter(new FileWriter("names5.txt"));
        for (Student s: list) {
            bw.write(s.toString());
            bw.newLine();
        }
        bw.close();

        //判断是否进行下一轮点名
        if(weight == 0){
            //没有这个，会报错
            ////Exception in thread "main" java.io.IOException: Stream closed
            bw = new BufferedWriter(new FileWriter("names5.txt"));
            for(int i = 0; i < list.size(); i++){
                student = list.get(i);
                student.setWeight(1.0);
                //for (Student s: list) {
                bw.write(student.toString());
                bw.newLine();
                //}
            }
            bw.close();
        }
    }
}
